package com.qiangesoft.mybatisflex.web;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 租户拦截器
 */
public class TenantInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 通过 request 去获取租户 ID
        Long tenantId = getTenantIdByRequest(request);

        // 设置租户ID到 request 的 attribute
        request.setAttribute("tenantId", tenantId);
        return true;
    }

    /**
     * 通过 request 获取租户ID
     * @param request
     * @return
     */
    private Long getTenantIdByRequest(HttpServletRequest request) {
        // todo 自定义实现获取租户ID
        return 1L;
    }

}